import {
  Controller,
  Get,
  Post,
  Body,
  UseGuards,
  Query,
  ParseIntPipe,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiQuery } from '@nestjs/swagger';
import { RechargeService } from './recharge.service';
import { CreateRechargeRuleDto } from './dto/create-recharge-rule.dto';
import { InitiateRechargeDto } from './dto/initiate-recharge.dto';
import { PaymentCallbackDto } from './dto/payment-callback.dto';
import { RechargeRule } from './entities/recharge-rule.entity';
import { UserRecharge } from './entities/user-recharge.entity';
import { JwtAuthGuard } from '../../../common/guards/jwt-auth.guard';
import { Public } from '../../../common/decorators/public.decorator';
import { User } from '../../../common/decorators/user.decorator';
import { User as UserEntity } from '../../users/entities/user.entity';

/**
 * 充值控制器
 */
@ApiTags('充值管理')
@Controller('member/recharge')
@UseGuards(JwtAuthGuard)
export class RechargeController {
  constructor(private readonly rechargeService: RechargeService) {}

  @ApiOperation({ summary: '发起充值' })
  @ApiResponse({ status: 201, description: '充值订单创建成功', type: UserRecharge })
  @Post('initiate')
  async initiateRecharge(
    @User() user: UserEntity,
    @Body() initiateDto: InitiateRechargeDto,
  ): Promise<UserRecharge> {
    return await this.rechargeService.initiateRecharge(user.id, initiateDto);
  }

  @ApiOperation({ summary: '支付回调处理' })
  @ApiResponse({ status: 200, description: '回调处理成功', type: UserRecharge })
  @Public()
  @Post('callback')
  async paymentCallback(@Body() callbackDto: PaymentCallbackDto): Promise<UserRecharge> {
    return await this.rechargeService.handlePaymentCallback(callbackDto);
  }

  @ApiOperation({ summary: '获取用户充值记录' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiQuery({ name: 'page', required: false, description: '页码', example: 1 })
  @ApiQuery({ name: 'limit', required: false, description: '每页数量', example: 10 })
  @Get('history')
  async getRechargeHistory(
    @User() user: UserEntity,
    @Query('page', new ParseIntPipe({ optional: true })) page = 1,
    @Query('limit', new ParseIntPipe({ optional: true })) limit = 10,
  ): Promise<{
    data: UserRecharge[];
    total: number;
    page: number;
    limit: number;
  }> {
    return await this.rechargeService.getUserRechargeHistory(user.id, page, limit);
  }

  @ApiOperation({ summary: '获取充值规则' })
  @ApiResponse({ status: 200, description: '获取成功', type: [RechargeRule] })
  @Public()
  @Get('rules')
  async getRechargeRules(): Promise<RechargeRule[]> {
    return await this.rechargeService.getRules();
  }

  @ApiOperation({ summary: '管理员获取所有充值规则' })
  @ApiResponse({ status: 200, description: '获取成功', type: [RechargeRule] })
  @Get('rules/admin')
  async getAllRechargeRules(): Promise<RechargeRule[]> {
    return await this.rechargeService.getAllRules();
  }

  @ApiOperation({ summary: '创建充值规则（管理员）' })
  @ApiResponse({ status: 201, description: '创建成功', type: RechargeRule })
  @Post('rules')
  async createRechargeRule(@Body() createDto: CreateRechargeRuleDto): Promise<RechargeRule> {
    return await this.rechargeService.createRule(createDto);
  }
}